const app = getApp()
Page({
  data: {
    authorize: false,
    dataSessionId:null,
    idIponeXFlag:false,
    giftList:[],
    formId:'',
    showCopyModal: false,
    showCodeModal: false,
    gameAppId:'',
    selectFlag:''
  },
  onLoad: function (options) {
    this.getGiftList();
  },
  onShow() {
    //审查token是否过期，如果过期，清空Storage需要再次登录
    app.effectiveToken();

    this.setData({
      idIponeXFlag: app.data.isIPhoneX
    })
    let session_id = wx.getStorageSync('sessionid_pro');
    if (!session_id) {
      this.setData({
        authorize: true
      })
    } else {
      this.setData({
        authorize: false
      })
    }
   
  },
  //请求获取福利的数据
  getGiftList(){
    let session_id = wx.getStorageSync('sessessionid_prosionId');
    wx.request({
      url: app.data.commonUrl + app.data.gift,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": 'Bearer ' + session_id,
      },
      method: 'GET',
      success: res => {
        if (res.data.code === 0) {
          this.setData({
            giftList: res.data.result
          })
        }
      }
    })
  },
  //登录请求
  getuserinfo(ops) {
    const loginParams = ops.detail;
    if (ops.detail.errMsg === "getUserInfo:fail auth deny") {
      wx.showToast({ title: '请点击允许以获取信息!', icon: 'none' })
      return;
    }
    app.callLogin(loginParams).then(data => {
      this.setData({
        authorize: false,
        dataSessionId: wx.getStorageSync('sessionid_pro'),
      });
      wx.hideLoading();
      wx.showToast({ title: '登录成功' });
      this.getGiftList() //点击登录发送请求获取数据
    })
  },
  //点击领取
  submit(e){
    let xyxid = e.detail.target.dataset.xyxid,
        appId = e.detail.target.dataset.appid,
        code = e.detail.target.dataset.code,
        id = e.detail.target.dataset.id,
        idx = e.detail.target.dataset.index,
      session_id = wx.getStorageSync('sessionid_pro'),
        tempGiftList = this.data.giftList;
    this.setData({
      formId: e.detail.formId,
      gameCode: code,
      gameAppId:appId
    })
    
    if (!session_id){
      this.setData({
        authorize:true
      })
    }else{
      this.pushNotificationFn(id);
      this.setData({
        showCodeModal: true
      })
      wx.request({
        url: app.data.commonUrl + app.data.gift,
        data: {
          xyxid:xyxid
        },
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          "Authorization": 'Bearer ' + session_id,
        },
        success: res => {
          if (res.data.code === 0) {
            this.getGiftList();
            this.setData({
              showCodeModal: true,
              giftList: tempGiftList
            })

          }
        }
      })
    }

  },
  //点击领取或者确定的按钮，每天把点击的第一次发送pushNotification请求
  pushNotificationFn(id){
    let day = new Date();
    day.setTime(day.getTime());
    let s2 = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate(),
        push_id = wx.getStorageSync('push_id_' + s2),
      session_id = wx.getStorageSync('sessionid_pro');
      
    if (push_id != id) {
      wx.request({
        url: app.data.commonUrl + app.data.pushNotification,
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          "Authorization": 'Bearer ' + session_id,
        },
        data: {
          token: this.data.formId,
          appflag: 'xcx06'
        },
        success: res => {
          console.log(res)
        }
      })
      wx.setStorageSync('push_id_' + s2, id)
    }
   
  },
  hideModal(){
    this.setData({
      showCopyModal:false,
      showCodeModal:false,
    })
  },
  //复制领取码
  copyText(e){
    let copy_data = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: copy_data,
      success: res => {
        this.setData({
          showCodeModal: false,
          showCopyModal:true
        })
      }
      
    })
  },
  //跳转至当前点击的小程序
  toGameCenter(tempAppId){
    this.hideModal()
    wx.navigateToMiniProgram({
      appId: this.data.gameAppId,
      path: '',
      success(res) {}
    })
    
  }
})
